<?php

session_start();

include "webmaster_connect.php";

$page_permission = 2;

include "webmaster_check_login.php";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer alerts</title>

<style type="text/css">
body {
	background-image: url(../images/wood.jpg);
	background-repeat: repeat-x;
	margin: 0px;
	background-position: left top;
	background-attachment: fixed;
	background-color: #000000;
	padding-top: 0px;
	padding-right: 0px;
	padding-bottom: 0px;
	padding-left: 0px;
}
</style>
<link href="../css/webmaster_guest_alerts.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="page_container">

<?php 

include "../admin_files/webmaster_header.php";
include "../admin_files/webmaster_navigation.php" 

?>

	<div id="content_container">

	<h1>Guest alerts:</h1>

	<form id="product_filter" action="webmaster_customer_alerts.php" method="post" name="alerts_filter">

	<label> Sent status: 
	
    <select name="alert_status">
		<option value="All">All</option>
		<option value="Active">Active</option>
		<option value="Sent">Sent</option>
	</select>

	</label>

	<?php

	$customer_email_list = array();

	$query = "SELECT * FROM customer_alerts";
	$result = mysql_query($query);
	while($row = mysql_fetch_array($result))
		
		{
		array_push($customer_email_list, $row['customer_email']);
		}

	$customer_email_list = array_unique($customer_email_list);

	//print_r($customer_email_list);

	?>

	<label> Email: 
	
    <select name="customer_email">
		<option value="All">All</option>

		<?php
		foreach ($customer_email_list as $value) 
			
			{
			echo '<option value="'.$value.'">'.$value.'</option>';
			}
			
		?>

	</select>
	
    </label>

	<input name="refresh_alerts" type="submit" value="Refresh" />

	</form>

	<table width="970px" border="0" cellpadding="3">
		<tr>
    		<th class="header" scope="col"></th>
    		<th class="header" scope="col">id</th>
    		<th class="header" scope="col">Created</th>
    		<th class="header" scope="col">Status</th>
    		<th class="header" scope="col">Product ID</th>
    		<th class="header" scope="col">Product name</th>
    		<th class="header" scope="col">Product status</th>
    		<th class="header" scope="col">Customer email</th>
    		<th class="header" scope="col">Alert sent</th>
  		</tr>

	<?php 
 
	$alert_filter = $_POST['alert_status'];
	$email_filter = $_POST['customer_email'];
 
	$query = "
		SELECT *
		FROM customer_alerts";

	$query .= "
		INNER JOIN saddles
		ON customer_alerts.product_id = saddles.product_id";

	if(isset($_POST['alert_status'])) //If status is posted then do below checks

		{
		if ($alert_filter != "All") //If filter selected is NOT "All"

			{
			$query .= ' WHERE status = "'.$_POST['alert_status'].'"';
		
			// Joine = Either WHERE or AND and is used in joining two queries together
			$joiner = "AND ";
			}
	
			else
		
			{
			$joiner = "WHERE";
			}
	
		}	

		if(isset($_POST['customer_email'])) //If status is posted then do below checks

		{
		if ($email_filter != "All") //If filter selected is NOT "All"

			{
			$query .= ' '.$joiner.' customer_email = "'.$_POST['customer_email'].'"';
			}
		
		}	

		$result = mysql_query($query);
		while($row = mysql_fetch_array($result))
		
			{
			if($row['alert_date'] != 0000-00-00)
				
				{
				$alert_date = $row['alert_sent'];
				$alert_date = date("d/m/Y", strtotime($alert_date));
				} 
				
				else
		
				{
				$alert_date = 'Not sent';
				}
	
			$alert_created = $row['alert_created'];
			$alert_created = date("d/m/Y", strtotime($alert_created));
	
			echo '

   			<tr>
    			<th>
				<form action="webmaster_process_remove_alert.php" method="post" name="remove_alert" id="remove_alerts">
				<input name="alert_id" type="hidden" value="'.$row['alert_id'].'" />
				<input name="remove_alert" type="submit" value="Remove" />
				</form>
				</th>
				<th>'.$row['alert_id'].'</th>
				<th>'.$row['alert_created'].'</th>
				<th>'.$row['status'].'</th>
				<th>'.$row['product_id'].'</th>
    			<th>'.$row['product_name'].'</th>
				<th>'.$row['product_status'].'</th>
    			<th>'.$row['customer_email'].'</th>
				<th>'.$alert_date.'</th>
  			</tr>

			';
		
			}


		?>

	</table>

	<?php 

	/////////////////////////
	//TESTING - Display query
	/////////////////////////

	//echo $query 

	/////////////
	//END TESTING
	/////////////
	?>

	</div>
	<!--login_container-->

</div> 
<!--page_container-->

</body>
</html>